﻿using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Text;
using System.Linq;
using System.Windows.Forms;
using DevExpress.XtraEditors;
using FlyHorse.LIMS.Model;
using FlyHorse.LIMS.WinApp.LanguageResource;

namespace FlyHorse.LIMS.WinApp.BaseManageForms.StandardSelfForms
{
    public partial class StandardSelfAddForm : DevExpress.XtraEditors.XtraForm
    {
        BLL.GoodBll goodsBll = new BLL.GoodBll();
        BLL.TestItemBll testItemBll = new BLL.TestItemBll();
        BLL.ToolBll toolBll = new BLL.ToolBll();
        BLL.StandardRawBll standardRawBll = new BLL.StandardRawBll();
        Action _InitGrid;
        StandardRaw _standardRaw;
        int _option;
        public StandardSelfAddForm(Action InitGrid)
        {
            InitializeComponent();
            _InitGrid = InitGrid;
            _option = 1;
        }

        public StandardSelfAddForm(StandardRaw standardRaw, Action InitGrid)
        {
            InitializeComponent();
            _standardRaw = standardRaw;
            _option = 2;
            _InitGrid = InitGrid;
        }

        private void StandardSelfAddForm_Load(object sender, EventArgs e)
        {
            if (_option == 1)
            {
                var goodsModels = toolBll.GetRawGoodList();
                var testModels = testItemBll.LoadEntities(m => true).ToList();
                this.searchLookUpEdit_goodsName.Properties.DataSource = goodsModels;
                this.searchLookUpEdit_TestItemName.Properties.DataSource = testModels;
            }
            if (_option == 2)
            {
                this.searchLookUpEdit_goodsName.Properties.DataSource = toolBll.GetAllGoodList();
                this.searchLookUpEdit_TestItemName.Properties.DataSource = toolBll.GetAllTestItemList();

                searchLookUpEdit_goodsName.EditValue = _standardRaw.GoodsID;
                searchLookUpEdit_goodsName.Enabled = false;
                searchLookUpEdit_TestItemName.EditValue = _standardRaw.TestItemID;
                searchLookUpEdit_TestItemName.Enabled = false;

                if (_standardRaw.MaxValue!=null)
                {
                    this.textEdit_MaxValue.Text = _standardRaw.MaxValue.ToString();
                }
                if ( _standardRaw.MinValue!=null)
                {
                    this.textEdit_minValue.Text = _standardRaw.MinValue.ToString();
                }
                this.textEdit_Order.Text = _standardRaw.FormOrder.ToString();
                _standardRaw.Remark = _standardRaw.Remark;
            }



        }

        private void simpleButton_save_Click(object sender, EventArgs e)
        {
            if (this.searchLookUpEdit_goodsName.EditValue == null || this.searchLookUpEdit_goodsName.EditValue.ToString() == "")
            {
                XtraMessageBox.Show(ResourceOverall.InputNull);
                this.searchLookUpEdit_goodsName.Focus();
                return;
            }
            if (this.searchLookUpEdit_TestItemName.EditValue == null)
            {
                XtraMessageBox.Show(ResourceOverall.InputNull);
                this.searchLookUpEdit_TestItemName.Focus();
                return;
            }
            if (this.textEdit_Order.Text == "")
            {
                XtraMessageBox.Show(ResourceOverall.InputNull);
                textEdit_Order.Focus();
                return;
            }
            if (_option == 1)
            {
                StandardRaw standardSelf = new StandardRaw();
                standardSelf.StandardRawID = Guid.NewGuid().ToString();
                try
                {
                    standardSelf.GoodsID = Convert.ToString(this.searchLookUpEdit_goodsName.EditValue);
                    standardSelf.TestItemID = Convert.ToString(this.searchLookUpEdit_TestItemName.EditValue);
                    standardSelf.FormOrder = Convert.ToInt32(this.textEdit_Order.Text);
                    if (this.textEdit_MaxValue.Text != "")
                    {
                        Convert.ToDouble(this.textEdit_MaxValue.Text);
                        standardSelf.MaxValue = this.textEdit_MaxValue.Text;
                    }
                    else
                    {
                        standardSelf.MaxValue = null;
                    }
                    if (textEdit_minValue.Text != "")
                    {
                        Convert.ToDouble(this.textEdit_minValue.Text);
                        standardSelf.MinValue = this.textEdit_minValue.Text;
                    }
                    else
                    {
                        standardSelf.MinValue = null;
                    }
                    standardSelf.Remark = this.textEdit_Remark.Text;
                }
                catch (Exception ex)
                {
                    XtraMessageBox.Show(ex.Message);
                    return;
                }
                //判重
                int standardCount = standardRawBll.LoadEntities(m => m.TestItemID == standardSelf.TestItemID && m.GoodsID == standardSelf.GoodsID).Count();
                if (standardCount > 0)
                {
                    XtraMessageBox.Show(ResourceOverall.DataExists);
                    return;
                }

                //最大值比最小值小
                if (standardSelf.MinValue != null && standardSelf.MaxValue != null)
                {
                    if ( Convert.ToDouble(standardSelf.MinValue) >  Convert.ToDouble(standardSelf.MaxValue))
                    {
                        XtraMessageBox.Show(ResourceOverall.ValueCompareErr);
                        return;
                    }
                }
                using (OJCB4Entities db =new OJCB4Entities())
                {
                    db.StandardRaws.Add(standardSelf);
                    bool res = db.SaveChanges()>0;
                    if (res)
                    {
                        XtraMessageBox.Show(ResourceOverall.CreateSucceed);
                        _InitGrid();
                        this.Close();
                    }
                }
                
            }
            if (_option == 2)
            {
                if (_standardRaw==null)
                {
                    return;
                }
                using (OJCB4Entities db = new OJCB4Entities())
                {
                    try
                    {
                        StandardRaw standardRaw = db.StandardRaws.Find(_standardRaw.StandardRawID);
                        if (standardRaw==null)
                        {
                            return;
                        }
                        standardRaw.FormOrder = Convert.ToInt32(this.textEdit_Order.Text);
                        if (this.textEdit_MaxValue.Text != "")
                        {
                            Convert.ToDouble(this.textEdit_MaxValue.Text);
                            standardRaw.MaxValue = this.textEdit_MaxValue.Text;
                        }
                        else
                        {
                            standardRaw.MaxValue = null;
                        }
                        if (textEdit_minValue.Text != "")
                        {
                            Convert.ToDouble(this.textEdit_minValue.Text);
                            standardRaw.MinValue = this.textEdit_minValue.Text;
                        }
                        else
                        {
                            standardRaw.MinValue = null;
                        }

                        standardRaw.Remark = this.textEdit_Remark.Text;
                        //最大值比最小值小
                        if (standardRaw.MinValue != null && standardRaw.MaxValue != null)
                        {
                            if (Convert.ToDouble(standardRaw.MinValue) > Convert.ToDouble(standardRaw.MaxValue))
                            {
                                XtraMessageBox.Show(ResourceOverall.ValueCompareErr);
                                return;
                            }
                        }
                        var result = db.SaveChanges()>0;
                        if (result)
                        {
                            XtraMessageBox.Show(ResourceOverall.EditSucceed);
                            _InitGrid();
                            this.Close();
                        }
                    }
                    catch (Exception ex)
                    {
                        XtraMessageBox.Show(ex.Message);
                        return;
                    }
                    
                }
               
            }
            

        }
    }
}